package uml;

import java.io.*;
import java.util.StringTokenizer;
import java.util.Stack;

public class Data {

    Stack<String> tokens = new Stack<String>();

    int i=0;
    int len;
    String[] line = new String[500];

    void ReadFile(String filename)
    {
        try {
            String temp;
            int j = 0;
            BufferedReader filnein = new BufferedReader(new FileReader(filename));
            while ((temp = filnein.readLine()) != null) {
                line[j] = temp;
                j++;
            }
            filnein.close();
        } catch (IOException e) {
            System.out.println("File not found");
        }
    }

    String[] GetClass(String filename) {
        ReadFile(filename);
        String results[];
        results = new String[10];
        int j = 0;
        int k = 0;
        boolean getname = false;
        while(line[j] != null) {
            StringTokenizer st = new StringTokenizer(line[j]," ");
            while (st.hasMoreTokens()) {
                String NextToken = st.nextToken();
                if(getname) {
                    len = NextToken.length();
                    for(int l=0; l<len; l++) {
                        char character = NextToken.charAt(l);
                        if(character=='{') {
                            NextToken = NextToken.substring(0, l); 
                            break;
                        }
                    }
                    results[k] = NextToken;
                    k++;
                    getname = false;
                }
                if(NextToken.equals("class")) {
                    getname = true;
                }
            }
            j++;
        }
        return results;
    }


    String[] GetMethods(String filename, String classname) {
        ReadFile(filename);
        String results[];
        results = new String[100];
        int j = 0;
        int k = 0;
        boolean checkname = false;
        boolean findmethod = false;
        boolean getname = false;
        boolean inclass = false;
        //int ileokraglych = 0;
        int counter = 0;
        while(line[j] != null) {
            StringTokenizer st = new StringTokenizer(line[j]," ");
            while (st.hasMoreTokens()) {
                String NextToken = st.nextToken();

                len = NextToken.length();

                if(inclass && counter==0) {
                    findmethod = false;
                }
                if(len==1 && findmethod){
                    char character = NextToken.charAt(0);
                    if(character=='{') {
                        counter++;
                        inclass = true;
                    }
                    else if(character=='}') counter--;
                }
                else if(findmethod) {
                    if(NextToken.charAt(len-1)=='(') {
                        //NextToken=NextToken.substring(0,len-1);
                        //results[k] = NextToken;
                        results[k] = NextToken.substring(0,len-1);
                        k++;
                    }
                }
                else if(checkname) {
                    if(NextToken.equals(classname)) {
                        findmethod = true;
                    }
                    checkname = false;
                }
                else if(NextToken.equals("class")) {
                    checkname = true;
                }
            }
            j++;
        }
        return results;
    }

    String[] GetMethods2(String filename, String classname) {
        ReadFile(filename);
        String results[];
        String tmp;
        results = new String[100];
        int j = 0;
        int k = 0;
        boolean checkname = false;
        boolean findmethod = false;
        boolean getname = false;
        boolean inclass = false;
        boolean moja = false;
        //int ileokraglych = 0;
        int counter = 0;
        tokens.setSize(5);
        String NextToken = null;
        while(line[j] != null) {
            StringTokenizer st = new StringTokenizer(line[j]," ");
            while (st.hasMoreTokens()) {
                NextToken = st.nextToken();
                tokens.insertElementAt(NextToken, 0);
                len = NextToken.length();

                if(checkname) {
                    len = classname.length();
                    String tmp2 = NextToken.substring(0,len);
                    if(tmp2.equals(classname)) {
                        findmethod = true;
                        moja = true;
                    }
                    checkname = false;
                }
                len = NextToken.length();

                if(inclass && counter==0) {
                    findmethod = false;
                    //return results;
                }
                else if(findmethod) {
                    if(moja) {
                        moja = false;
                    }
                    for(int l=0; l<len; l++) {
                        char character = NextToken.charAt(l);
                        if(character=='(') {
                            if(l==0) {
                                tmp = tokens.get(1);
                            }
                            else {
                                tmp=NextToken.substring(0,l);
                            }
                            if(tmp.equals("if") || tmp.equals("while") || tmp.equals("do")) {
                            } else {
                                results[k]=tmp;
                                k++;
                            }
                        }
                        else if(character=='{') {
                            counter++;
                            //System.out.println(tokens.get(0)+" "+counter);
                            inclass = true;
                        }
                        else if(character=='}') {
                            counter--;
                            //System.out.println(tokens.get(0)+" "+counter);
                        }
                    }
                }
                else if(NextToken.equals("class")) {
                    checkname = true;
                }
            }
            j++;
        }
        return results;
    }

    void Funkcja() {
        ReadFile("test.txt");
        String tabele1[];
        String tabele2[];
        tabele1 = GetClass("test.txt");
        tabele2 = GetMethods2("test.txt", tabele1[0]);
        System.out.println(tabele1[0]);
        System.out.println(tabele1[1]);
        System.out.println(tabele1[2]);
        //System.out.println(tabele1[3]);
        System.out.println(tabele2[0]);
        System.out.println(tabele2[1]);
        System.out.println(tabele2[2]);
        System.out.println(tabele2[3]);
    }
}
